/**
 * 
 */
package com.android.vtca.sudoky;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;

public class keypad extends Dialog {

	private final Button vKey[] = new Button[9];
	private TableLayout vkeypad;
	private final int iUsed[];
	private final GamePlay GPlay;

	public keypad(Context context, int iUseds[], GamePlay mGamePlay) {
		super(context);
		this.iUsed = iUseds;
		this.GPlay = mGamePlay;

	}

	public void returnResult(int tile) {
		GPlay.setSelectedTile(tile);
		dismiss();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setTitle(R.string.keypad_title);
		setContentView(R.layout.layout_keypad_diglog);
		findView();

		for (int i = 0; i < iUsed.length; i++) {

			if (iUsed[i] != 0) {
				vKey[iUsed[i] - 1].setVisibility(View.INVISIBLE);
			}

		}
		setListeners();
	}

	private void findView() {
		vkeypad = (TableLayout) findViewById(R.id.keypad);
		vKey[0] = (Button) findViewById(R.id.keypad_1);
		vKey[1] = (Button) findViewById(R.id.keypad_2);
		vKey[2] = (Button) findViewById(R.id.keypad_3);
		vKey[3] = (Button) findViewById(R.id.keypad_4);
		vKey[4] = (Button) findViewById(R.id.keypad_5);
		vKey[5] = (Button) findViewById(R.id.keypad_6);
		vKey[6] = (Button) findViewById(R.id.keypad_7);
		vKey[7] = (Button) findViewById(R.id.keypad_8);
		vKey[8] = (Button) findViewById(R.id.keypad_9);
	}

	private void setListeners() {
		vkeypad.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				returnResult(0);
			}
		});
		for (int i = 0; i < vKey.length; i++) {
			final int t = i + 1;
			vKey[i].setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					returnResult(t);
				}
			});
		}
	}

}
